addEvent("onload", init);
addEvent("onresize", checkOrientation);
var screenmode = 0;

var filter;
function init(){
    try {
    
        var myStyleTweaks = new StyleTweaker();
        myStyleTweaks.add("Series60/5.0", "styles/tweaks/S605th.css");
        myStyleTweaks.add("Series60/3.2", "styles/tweaks/S603rdFP2.css");
        initEvents();
		show_secondary_filters('call');
    } 
    
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

function checkOrientation(){
    try {
        var width = parseInt(screen.width);
        var height = parseInt(screen.height);
        
        //var landscapeTweaker = new StyleTweaker();
        //landscapeTweaker.add("Series60/5.0", "../resources/styles/tweaks/S605thLandscape.css");
        //landscapeTweaker.add("Series60/3.2", "../resources/styles/tweaks/S603rdFP2Landscape.css");
        
        if (width > height) {
            // if already in landscape mode, return
            if (screenmode == 1) 
                return;
            
            screenmode = 1;
        }
        else {
            // if already in portrait mode, return
            if (screenmode == 0) 
                return;
            screenmode = 0;
        }
    } 
    
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 * Displays the list of events based on events data.
 * eventsiterator - iterator for handling ivents items
 */
function DisplayEvents(eventsiterator){
	try {
        var tablestart = "<table id='eventTable'><tr><th scope='col'>Phone</th><th scope='col'>Time</th>";
        var tableend = "</table>";
        var item, lastItem = null;
        var content = "";
        
        while ((item = eventsiterator.next()) != null) {
			lastItem = item;
            content += "<tr>";
            content += "<td>" + item.phoneNumber + "</td>" + "<td>" + item.time + "</td>";
			if(item.type=="SMS")
				content += "<td>" + item.summary + "</td>";
            content += "</tr>";
        }
		if(lastItem!=null){
			tablestart += lastItem.type=="SMS"?"<th scope='col'>Summary</th></tr>":"</tr>";
	        document.getElementById("eventscontainer").innerHTML = tablestart + content + tableend;
		}else
			document.getElementById("eventscontainer").innerHTML = "<br />" + createNoEventsMessage();;
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 * Highlights specific tabs
 */
function selectTab(first, second){
	document.getElementById("a1").className="";
	document.getElementById("a2").className="";
	document.getElementById("a11").className="";
	document.getElementById("a12").className="";
	document.getElementById("a13").className="";
	document.getElementById("a21").className="";
	document.getElementById("a22").className="";
	
	document.getElementById(first).className="selectedTab";
	document.getElementById(second).className="selectedTab";
}

/*
 * Displays secondary filters container depending on type
 * type - filter type: call or sms
 */
function show_secondary_filters(type){
    try {
        if (type == "call") {
            document.getElementById("sms_filters").style.display = "none";
            document.getElementById("call_filters").style.display = "block";
			selectTab("a1", "a11");
			filter = createFilter("Call", "Outgoing");
            GetEvents(filter);
        }
        else {
            document.getElementById("sms_filters").style.display = "block";
            document.getElementById("call_filters").style.display = "none";
			selectTab("a2", "a21");
			filter = createFilter("SMS", "Received");
            GetEvents(filter);
        }
   } 
    catch (e) {
        alert(e);
    }
    
}

/*
 * Handles click of secondary filter - lists events depending on users choice
 * type - type of events: SMS or Call
 * flag - flag of event: for Calls: Outgoing, Missed, Incoming. For SMS: Incoming, Outgoing
 */
function onclick_secondary_filter(type, flag){
    try {
        filter = createFilter(type, flag);
        GetEvents(filter);
    } 
    catch (e) {
        alert(e);
    }
}


function createNoEventsMessage(){
	return "No " + filter.flag.toLowerCase() + " " + (filter.type=="SMS"?"messages":"calls");
}
